import { Component, OnInit } from '@angular/core';
import {ViewChild, AfterViewInit} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {isString} from 'util';
import { FormsModule} from '@angular/forms';
import { StorageService} from '../services/storage.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit, AfterViewInit {
// 手机号码提示
public numText: any = '手机号不正确';
// 是否提示手机错误信息
public errorlist: any = false;
// 手机号码
public numlist: any;
// 验证码发送
public ButtonText: any = '发送验证码';
// 剩余时间
public time: any = 60;
// 输入验证码
public Verification: any;
// 判断按钮样式是否改变
public off: any = false;
// 判断短信发送条件
public prompt1: any = false;
// 验证码错误
public numText2: any;
  constructor( public http: HttpClient, public storage: StorageService, public router: Router) { }

  ngOnInit() {
    // console.log(this.storage.get());
  }
  ngAfterViewInit() {

  }
  //
// 手机号报错
error1(e) {
  console.log(this.numlist);
  if ( this.numlist.toString().length < 11) {
   this.errorlist = true;
  } else {
    this.errorlist = false;
  }
 }
 // 验证码发送
 getCode(e) {
    // if ( this.errorlist = false) {
   //     //   const start = setInterval(
   //     //     // 设置信息显示时长与按钮字幕样式
   //     //     () => {
   //     //       if (this.time >= 0 ) {
   //     //         this.ButtonText = '已发送' + '(' + this.time-- + ')';
   //     //         if (this.time < 60) {
   //     //           this.prompt1 = true;
   //     //           if (this.time < 57) {
   //     //             this.prompt1 = false;
   //     //           }
   //     //         }
   //     //       } else {
   //     //         clearInterval(start);
   //     //         this.ButtonText = '重新发送';
   //     //         this.time = 60;
   //     //         this.off = false;
   //     //       }
   //     //     }, 1000);
   //     // } else {
   //     //   this.off = true;
   //     // }

    // 获取验证码
    const httpoptions = {
      headers: new HttpHeaders({'Content-Type': 'application/json'})};
    this.http.post('http://192.168.2.227:8080/user/sendpin', { u_tel: this.numlist },
      httpoptions).subscribe((respnse: any) => {
      console.log(respnse);
      console.log(respnse.data);
      // 通过返回code判断手机号是否合法
      if ( respnse.code === 1025) {
        console.log('错误');
        this.numlist = '';
        this.errorlist = true ;
        this.numText = '手机号码不存在' ;
      }  else {
        console.log('正确');
        console.log(this.Verification)
        this.errorlist = false ;
        const start = setInterval(
                // 设置信息显示时长与按钮字幕样式
                () => {
                  if (this.time >= 0 ) {
                    this.ButtonText = '已发送' + '(' + this.time-- + ')';
                    // 提示框开关
                    if (this.time < 60) {
                      this.prompt1 = true;
                      if (this.time < 30) {
                        this.prompt1 = false;
                      }
                    }
                  } else {
                    clearInterval(start);
                    this.ButtonText = '重新发送';
                    this.time = 60;
                    this.off = false;
                  }
                }, 1000);
        this.off = true;
      }
    });
}
send() {
    setInterval(() => {this.router.navigate(['/shops/']) }, 1000);

    console.log('发送');
    console.log(this.Verification);
    const sub = {
    headers: new HttpHeaders({'Content-Type': 'application/json'})};
    this.http.post('http://192.168.2.227:8080/user/login', { u_tel: this.numlist, u_cheaknumber: this.Verification},
    sub).subscribe((respnse: any) => {
      console.log(respnse);
      if (respnse.code === 0) {
        this.numText2 = '';
        console.log(this.numText2);
        // 缓存id到本地
        console.log(respnse.data.u_id)
        this.storage.set('id', respnse.data.u_id);
      } else {
        this.numText2 = '验证码已过期或错误';
      }
    });
}
}


